home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / rpc.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  266 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    rpc -
  19.  *        Put different things to a stream: floats, ints, strings, 
  20.  *    and newlines.
  21.  *
  22.  *                Paul Haeberli - 1985
  23.  */
  24. #include "stdio.h"
  25. #include "ctype.h"
  26.  
  27. putfloat(file,f)
  28. FILE *file;
  29. float f;
  30. {
  31.     int intpart;
  32.     int fracpart;
  33.     char tempbuf[40]; 
  34.     char *cptr;
  35.     int i;
  36.  
  37.     if(f<0.0) {
  38.        putc('-',file);
  39.        f = -f;
  40.     }
  41.     intpart = f;
  42.     cptr = tempbuf;
  43.     do {
  44.     *cptr++ = '0'+(intpart % 10);
  45.     intpart /= 10;
  46.     } while(intpart);
  47.     while(cptr != tempbuf) {
  48.     cptr--;
  49.     putc(*cptr,file);
  50.     }
  51.     putc('.',file);
  52.     fracpart = 1000000*(f-intpart);
  53.     if(fracpart != 0) {
  54.     cptr = tempbuf;
  55.     for(i=0; i<6; i++) {
  56.         if((fracpart % 10) != 0)
  57.         break;
  58.         fracpart /= 10;
  59.     }
  60.     if(i == 6) 
  61.         putc('0',file);
  62.     else {
  63.         for(;i<6; i++) {
  64.         *cptr++ = '0'+(fracpart % 10);
  65.         fracpart /= 10;
  66.         }
  67.         while(cptr != tempbuf) {
  68.         cptr--;
  69.         putc(*cptr,file);
  70.         }
  71.     }
  72.     } else 
  73.     putc('0',file);
  74.     putc(' ',file);
  75. }
  76.  
  77. putint(file,i)
  78. FILE *file;
  79. int i;
  80. {
  81.     char tempbuf[40]; 
  82.     char *cptr;
  83.  
  84.     if(i<0.0) {
  85.        putc('-',file);
  86.        i = -i;
  87.     }
  88.     cptr = tempbuf;
  89.     do {
  90.     *cptr++ = '0'+(i % 10);
  91.     i /= 10;
  92.     } while(i);
  93.     while(cptr != tempbuf) {
  94.     cptr--;
  95.     putc(*cptr,file);
  96.     }
  97.     putc(' ',file);
  98. }
  99.  
  100. putstr(file,str)
  101. FILE *file;
  102. char *str;
  103. {
  104.     char tempbuf[40]; 
  105.     char *cptr;
  106.  
  107.     while(*str) 
  108.        putc(*str++,file);
  109.     putc(' ',file);
  110. }
  111.  
  112. putnewline(file)
  113. FILE *file;
  114. {
  115.     putc('\n',file);
  116. }
  117.  
  118. /*
  119.  *    get different things from a stream
  120.  *
  121.  *    floats, ints, strings, newlines.
  122.  *
  123.  */
  124. float getfloat(file)
  125. FILE *file;
  126. {
  127.     int c;
  128.     int intpart;
  129.     int fracpart;
  130.     float retval;
  131.     int cnt;
  132.     int isneg;
  133.  
  134.     c = getc(file); 
  135.     while(c != EOF && isspace(c))
  136.     c = getc(file); 
  137.     if(c == '-') {
  138.     isneg = 1;
  139.     c = getc(file); 
  140.     } else { 
  141.     isneg = 0;
  142.     if(c == '+') 
  143.         c = getc(file); 
  144.     }
  145.     intpart = 0;
  146.     fracpart = 0;
  147.     retval = 0.0;
  148.     while(isdigit(c)) { 
  149.     intpart = 10*intpart+(c-'0');
  150.     c = getc(file);
  151.     }
  152.     if(c == '.') {
  153.     c = getc(file);
  154.     cnt = 0;
  155.     while(isdigit(c)) {
  156.         fracpart = 10*fracpart+(c-'0');
  157.         c = getc(file);
  158.         cnt++;
  159.     }
  160.         switch(cnt) {
  161.         case 0:
  162.         retval = 0.0; 
  163.         break;
  164.         case 1:
  165.         retval = fracpart/10.0;
  166.         break;
  167.         case 2:
  168.         retval = fracpart/100.0;
  169.         break;
  170.         case 3:
  171.         retval = fracpart/1000.0;
  172.         break;
  173.         case 4:
  174.         retval = fracpart/10000.0;
  175.         break;
  176.         case 5:
  177.         retval = fracpart/100000.0;
  178.         break;
  179.         case 6:
  180.         retval = fracpart/1000000.0;
  181.         break;
  182.         case 7:
  183.         retval = fracpart/10000000.0;
  184.         break;
  185.         case 8:
  186.         retval = fracpart/100000000.0;
  187.         break;
  188.         case 9:
  189.         retval = fracpart/1000000000.0;
  190.         break;
  191.         case 10:
  192.         retval = 0.0; 
  193.         break;
  194.         default:
  195.         printf("getfloat: too many digits after decimal point\n");
  196.         break;
  197.     }
  198.     }
  199.     if(c == '\n')
  200.        ungetc(c,file);
  201.     if(isneg)
  202.     return -(retval+intpart);
  203.     else
  204.     return  (retval+intpart);
  205. }
  206.  
  207. getint(file)
  208. FILE *file;
  209. {
  210.     int c, val;
  211.     int isneg;
  212.  
  213.     c = getc(file);
  214.     while(isspace(c)) 
  215.     c = getc(file);
  216.     if(c == '-') {
  217.     isneg = 1;
  218.     c = getc(file);
  219.     } else {
  220.     isneg = 0;
  221.     if(c == '+') 
  222.         c = getc(file);
  223.     }
  224.     val = 0;
  225.     while (isdigit(c)) {
  226.     val = 10*val + (c-'0');
  227.     c = getc(file);
  228.     }
  229.     if(c == '\n')
  230.        ungetc(c,file);
  231.     if(isneg)
  232.     return -val;
  233.     else
  234.     return val;
  235. }
  236.  
  237. getstr(file,str)
  238. FILE *file;
  239. char *str;
  240. {
  241.     int c;
  242.  
  243.     c = getc(file);
  244.     if(c != '\n') {
  245.     while(isspace(c)) 
  246.         c = getc(file);
  247.     while (c != EOF && !isspace(c)) {
  248.         *str++ = c;
  249.         c = getc(file);
  250.     }
  251.     if(c == '\n')
  252.        ungetc(c,file);
  253.     }
  254.     *str = 0;
  255.     return (c!=EOF);
  256. }
  257.  
  258. getnewline(file)
  259. FILE *file;
  260. {
  261.     int c;
  262.     c = getc(file);
  263.     while (c != EOF && c != '\n') 
  264.     c = getc(file);
  265. }
  266.